% \iffalse meta-comment
% minted.dtx
% Copyright 2010-2011 Konrad Rudolph
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% Additionally, the project may be distributed under the terms of the new BSD
% license.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Konrad Rudolph.
%
% This work consists of the files mints.dtx and mints.ins
% and the derived file mints.sty.
%
% Nanos gigantum humeris insidentes.
%
% Special thanks to Philipp Stephani and the rest of the guys from
% comp.text.tex and tex.stackexchange.com
% \fi
% \iffalse
%<*driver>
\ProvidesFile{minted.dtx}
%</driver>
%<*package>
\NeedsTeXFormat{LaTeX2e}
%
\ProvidesPackage{minted}[2011/09/17 v1.7 Yet another Pygments shim for LaTeX]
%
\RequirePackage{keyval}
\RequirePackage{fancyvrb}
\RequirePackage{xcolor}
\RequirePackage{float}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{ifplatform}
%</package>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\DisableCrossrefs
\RecordChanges
%\OnlyDescription

\usepackage{lstdoc}
\usepackage{minted}
\usepackage{courier} ^^A Useful monospace font (= has \bfseries).
\usepackage{textcomp}
\usepackage{hyperref}
\usepackage{microtype}

\def\MacroFont{%
  \fontencoding\encodingdefault%
  \fontfamily\ttdefault%
  \fontseries\mddefault%
  \fontshape\updefault%
  \small}

^^A FIXME This is incredibly hacky and overrides size commands around tt-text.
^^A But I don't get the font inside the main body to change otherwise. :-(
\let\mintedttold\ttfamily
\def\ttfamily{\mintedttold\fontsize{9}{9}\selectfont}

\definecolor{minted@mint}{HTML}{4B6121}
\definecolor{minted@samplebg}{HTML}{F0F0E0}
\colorlet{minted@linkcolor}{minted@mint}

\def\PrintDescribeMacro#1{\strut \MacroFont\textcolor{minted@linkcolor}{\string #1\ }}
\let\PrintDescribeEnv\PrintDescribeMacro
\let\PrintMacroName\PrintDescribeMacro
\let\PrintEnvName\PrintDescribeEnv
\def\theCodelineNo{\textcolor{minted@linkcolor}{\sffamily\footnotesize\oldstylenums{\arabic{CodelineNo}}}}

\hypersetup{
  pdftitle=The minted package: Highlighted source code in LaTeX,
  pdfauthor=Konrad Rudolph,
  pdfsubject={Minted LaTeX package manual},
  allcolors=minted@linkcolor,
}

^^A Shamelessly stolen from http://blog.karssen.org/2009/11/15/a-latex-example-environment/
\newenvironment{example}
  {\VerbatimEnvironment
   \begin{VerbatimOut}[gobble=3]{example.out}}
  {\end{VerbatimOut}
   \vspace{1ex}
   \setlength{\parindent}{0pt}
   \fbox{\begin{minipage}{0.5\linewidth}
     \inputminted[resetmargins]{latex}{example.out}
   \end{minipage}
   \hspace{0.05\linewidth}
   \begin{minipage}{0.4\linewidth}
     \input{example.out}
   \end{minipage}
   \vspace{1ex}}}

\makeatletter ^^A ?!? Needed, no idea why.
\def\minted@printopt#1(#2) (#3){%
  \leavevmode%
  \marginpar{\raggedleft\texttt{\textcolor{minted@linkcolor}{#1}}\ }%
  \textsf{(#2)}\hfill(default: #3)\\}

\newenvironment{optionlist}{%
  \par%
  \newcommand*\mintednext{}%
  \renewcommand*\item[1][]{%
    \mintednext%
    \renewcommand*\mintednext{\par}%
    \minted@printopt##1%
    \ignorespaces}
  \DeleteShortVerb{\|}%
  \MakeShortVerb{\+}}{%
  \DeleteShortVerb{\+}%
  \MakeShortVerb{\|}%
  \par}

\newcommand\thestyle{trac}
\usemintedstyle{\thestyle}

\setlength{\parindent}{0pt}
\addtolength{\parskip}{0.5\baselineskip}

\begin{document}
\DocInput{minted.dtx}
\end{document}
%</driver>
% \fi
%
% \changes{0.0.4}{2010/01/08}{Initial version}
% \changes{1.6}{2010/01/22}{Simpler versioning scheme}
%
% \GetFileInfo{minted.sty}
%
% \newcommand\pkg[1]{\textsf{#1}}
% \newcommand\app[1]{\textsf{#1}}
%
% \title{The \textcolor{minted@mint}{\pkg{minted}} package:\\Highlighted source code in \LaTeX
%   \thanks{This document corresponds to \pkg{minted}~\fileversion, last changed~\filedate.}}
% \author{Konrad Rudolph\\\texttt{konrad\char`\_rudolph@madrat.net}}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
%   \noindent\pkg{minted} is a package that facilitates expressive syntax highlighting
%   using the powerful \app{Pygments} library. The package also provides options to
%   customize the highlighted source code output.
% \end{abstract}
%
% \begin{multicols}{2}
%   \setlength{\parskip}{0pt}
%   \tableofcontents
% \end{multicols}
%
% \fvset{
%   codes={\catcode`\%=9}, ^^A Ignore initial |%|
%   numbersep=5pt,
%   fontsize=\small
% }
% \setlength{\fboxsep}{1ex}
%
% \mbox{}\newpage
% \section{Introduction}
%
% \pkg{minted} is a package that allows formatting source code in \LaTeX.
% For example:
% \begin{VerbatimOut}[gobble=1]{minted.doc.out}
%   \begin{minted}{language}
%     code
%   \end{minted}
% \end{VerbatimOut}
% \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out}
%
% will highlight a piece of code in a chosen language.
% The display can be customized by a number of arguments and colour schemes.
%
% Unlike some other packages, most notably \pkg{listings}, \pkg{minted} requires
% the installation of an additional software, \app{Pygments}.
% This may seem like a disadvantage but there are advantages, as well:
%
% \app{Pygments} provides far superior syntax highlighting compared to conventional packages.
% For example, \pkg{listings} basically only highlights strings, comments and keywords.
% \app{Pygments}, on the other hand, can be completely customized to highlight any token kind the
% source language might support.
% This might include special formatting sequences inside strings, numbers, different kinds of
% identifiers and exotic constructs such as HTML tags.
%
% Some languages make this especially desirable.
% Consider the following Ruby code as an extreme, but at the same time typical, example:
%
% \begin{minted}[gobble=3]{ruby}
%   class Foo
%     def init
%       pi = Math::PI
%       @var = "Pi is approx. #{pi}"
%     end
%   end
% \end{minted}
%
% Here we have four different colors for identifiers (five, if you count keywords) and escapes from
% inside strings, none of which pose a problem to \app{Pygments}.
%
% Additionally, installing \app{Pygments} is actually incredibly easy (see the next section).
%
%
% \section{Installation}
%
% \subsection{Prerequisites}
%
% \app{Pygments} is written in Python so make sure that at least Python 2.6 is installed on you system:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ python --version
%   Python 2.6.2
% \end{Verbatim}
%
% If that's not the case, you can download it from \href{http://www.python.org/download/}{the website} or
% use your operating system's package manager.
%
% Next, install \href{http://pypi.python.org/pypi/setuptools}{\app{setuptools}} which facilitates the distribution of Python applications.
%
% You can then install \app{Pygments} using the following simple command:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ sudo easy_install Pygments
% \end{Verbatim}
%
% (If you've already got \app{Pygments} installed, be advised that \pkg{minted} requires at least version
% 1.2.)
%
%
% \subsection{Required packages}
%
% \pkg{minted} requires the following packages to be available and reasonably up to date on your system, all of which ship with recent \TeX{} distributions:
%
% \begingroup
% \setlength\parskip{0pt}
% \setlength\topsep{0pt}
% \begin{list}{\textrm{\labelitemi}}{\ttfamily}
%   \item keyval
%   \item fancyvrb
%   \item xcolor
%   \item float
%   \item ifthen
%   \item calc
%   \item ifplatform
% \end{list}
% \endgroup
%
%
% \subsection{Installing \pkg{minted}}
% \changes{1.7}{2010/03/16}{Installation instructions added}
%
% If the file |minted.sty| doesn't exist yet, we first have to create this.
% If you're using a system that supports the \app{make} command, then you can simply type the following
% command in the folder where you've extracted the \pkg{minted} package code:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ make
% \end{Verbatim}
%
% Alternatively, you may download this file separately from the
% \href{http://code.google.com/p/minted/downloads/list}{project's homepage}, or create it manually by
% executing the command
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ tex minted.ins
% \end{Verbatim}
%
% on the command line.
%
% We now have to install the file so that \TeX{} is able to find them.
% In order to do that, please refer to the
% \href{http://www.tex.ac.uk/cgi-bin/texfaq2html?label=inst-wlcf}{\TeX{} FAQ} on that subject.
%
%
% \subsection{Windows}
%
% Windows support is sketchy  / untested at the moment.
% There are two complications: installation and usage.
%
% \begin{description}
%   \item[Installation]
%     The above setting assumes that |easy_install| is in a path that Windows automatically find.
%     to do this, you must usually set your |PATH| environment variable accordingly (e.g. to
%     |C:\Python26\Scripts|).
%   \item[Usage]
%     \app{Pygments} currently does not ship with a Windows compatible application.
%     In order to still run it, you need to create a small command script and put it someplace where
%     Windows will find it (e.g. the aforementioned |Scripts| directory, which you will have registered
%     in the |PATH| variable anyway).
%     The script needs to be called |pygmentize.cmd| and it needs to contain the following content:
%
%     \def\mintedbs#1{\char`\\}
%     \def\mintedpcc#1{\char37\relax}
%     \begin{Verbatim}[gobble=7,commandchars=\\\{\}]
%       @echo off
%       set PYTHONPATH=C:\mintedbs{}Python26
%       \mintedpcc{}PYTHONPATH\mintedpcc{}\mintedbs{}python.exe \mintedpcc{}PYTHONPATH\mintedpcc{}\mintedbs{}Scripts\mintedbs{}pygmentize \mintedpcc{}*
%     \end{Verbatim}
% \end{description}
%
%
% \section{Basic usage}
%
% \subsection{Preliminary}
%
% Since \pkg{minted} makes calls to the outside world (i.e. \app{Pygments}), you need to tell the
% \LaTeX{} processor about this by passing it the |-shell-escape| option or it won't allow such calls.
% In effect, instead of calling the processor like this:
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ latex input
% \end{Verbatim}
%
% you need to call it like this:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ latex -shell-escape input
% \end{Verbatim}
%
% The same holds for other processors, such as |pdflatex| or |xelatex|.
%
%
% \subsection{A minimal complete example}
% \changes{1.7}{2010/03/16}{Minimal working example added}
%
% The following file |minimal.tex| shows the basic usage of \pkg{minted}.
%
% \begin{VerbatimOut}[gobble=1]{minted.doc.out}
%   \documentclass{article}
%
%   \usepackage{minted}
%
%   \begin{document}
%   \begin{minted}{c}
%   int main() {
%       printf("hello, world");
%       return 0;
%   }
%   \end{minted}
%   \end{document}
% \end{VerbatimOut}
% \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out}
%
% By compiling the source file like this:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ pdflatex -shell-escape minimal
% \end{Verbatim}
%
% we end up with the following output in |minimal.pdf|:
%
% \hfill
% \colorbox{minted@samplebg}{\begin{minipage}{0.6\textwidth}
%   \inputminted[firstline=7,lastline=10]{c}{minted.doc.out}
% \end{minipage}}
% \hfill\hfill
%
% \subsection{Formatting source code}
%
% \DescribeEnv{minted}
% Using \pkg{minted} is straightforward. For example, to highlight a Python source code we might use
% the following code snippet (result on the right):
%
% \begin{example}
%   \begin{minted}{python}
%   def boring(args = None):
%       pass
%   \end{minted}
% \end{example}
%
% Optionally, the environment accepts a number of options in |key=value| notation, which are described
% in more detail below.
%
% \DescribeMacro{\mint}
% For one-line source codes, you can alternatively use a shorthand notation similar to |\verb|:^^A|
%
% \begin{example}
%   \mint{python}|import this|
% \end{example}
%
% The complete syntax is \cmd\mint\oarg{options}\marg{language}|/|\meta{code}|/|,
% where the code delimither |/|, like with |\verb|, can be almost any punctuation character. ^^A|
% Again, this command supports a number of options described below.
%
% \DescribeMacro{\inputminted}
% Finally, there's the comment |\inputminted| command to read and format whole files.
% Its syntax is \cmd\inputminted\oarg{options}\marg{language}\marg{filename}.
%
%
% \subsection{Using different styles}
%
% Instead of using the default style you may choose an another stylesheet provided by \app{Pygments}
% by its name.
% For example, this document uses the ``\texttt{\thestyle}'' style.
% \DescribeMacro{\usemintedstyle}
% To do this, put the following into the prelude of your document:
%
% \mint[frame=lines]{latex}/\usemintedstyle{name}/
%
% To get a list of all available stylesheets, execute the following command on the command line:
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ pygmentize -L styles
% \end{Verbatim}
%
% Creating own styles is also very easy. Just follow the instructions provided on the
% \href{http://pygments.org/docs/styles/#creating-own-styles}{website}.
%
%
% \subsection{Supported languages}
%
% \app{Pygments} at the moment supports over 150 different programming languages, template languages
% and other markup languages.
% To see an exhaustive list of the currently supported languages, use the command
%
% \begin{Verbatim}[gobble=3,commandchars=\\\{\}]
%   \$ pygmentize -L lexers
% \end{Verbatim}
%
%
% \section{Floated listings}\label{sec:float}
%
% \DescribeEnv{listing}
% \pkg{minted} provides the |listing| environment to wrap around a source code block.
% That way, the source code will be put into a floating box.
% You can also provide a |\caption| and a |\label| for such a listing in the usual way (that is,
% as for the |table| and |figure| environments):
% \begin{VerbatimOut}[gobble=1]{minted.doc.out}
%   \begin{listing}[H]
%     \mint{cl}/(car (cons 1 '(2)))/
%     \caption{Example of a listing.}
%     \label{lst:example}
%   \end{listing}
%
%   Listing \ref{lst:example} contains an example of a listing.
% \end{VerbatimOut}
% \inputminted[gobble=2,frame=lines]{latex}{minted.doc.out}
%
% will yield:
%
% \hfill
% \colorbox{minted@samplebg}{\begin{minipage}{0.6\textwidth}
%   \input{minted.doc.out}
% \end{minipage}}
% \hfill\hfill
%
% \DescribeMacro{\listoflistings}
% The |\listoflistings| macro will insert a list of all (floated) listings into the document:
%
% \begin{example}
%   \listoflistings
% \end{example}
%
% \DescribeMacro{\listingscaption}
% The string ``Listing'' in a listing's caption can be changed.
% To do this, simply redefine the macro |\listingscaption|, e.g.:
%
% \mint[frame=lines]{latex}/\renewcommand\listingscaption{Program code}/
%
% \DescribeMacro{\listoflistingscaption}
% Likewise, the caption of the listings list, ``List of listings'' can be changed by redefining
% |\listoflistingscaption| like so:
%
% \mint[frame=lines]{latex}/\renewcommand\listoflistingscaption{List of program codes}/
%
%
% \section{Options}
%
%
% \subsection{Package options}
%
% \DescribeMacro{section}
% \DescribeMacro{chapter}
% To control how \LaTeX{} counts the |listing| floats, you can pass either the
% |section| or |chapter| option when loading the \pkg{minted} package.
% For example, the following will cause listings to be counted per-section:
%
% \mint[frame=lines]{latex}/\usepackage[section]{minted}/
%
%
% \subsection{Macro option usage}
%
% All \pkg{minted} highlight commands accept the same set of options.
% Options are specified as a comma-separated list of |key=value| pairs.
% For example, we can specify that the lines should be numbered:
%
% \begin{example}
%   \begin{minted}[linenos=true]{c++}
%   #include <iostream>
%   int main() {
%       std::cout << "Hello "
%                 << "world"
%                 << std::endl;
%   }
%   \end{minted}
% \end{example}
%
% An option value of |true| may also be omitted entirely (including the ``|=|'').
% To customize the display of the line numbers further, override the |\theFancyVerbLine| command.
% Consult the \pkg{fancyvrb} documentation for details.
%
% |\mint| accepts the same options:
%
% \begin{example}
%   \mint[linenos]{perl}|$x=~/foo/|
% \end{example}
%
% Here's another example: we want to use the \LaTeX{} math mode inside comments:
%
% \begin{example}
%   \begin{minted}[mathescape]{python}
%   # Returns $\sum_{i=1}^{n}i$
%   def sum_from_one_to(n):
%       r = range(1, n + 1)
%       return sum(r)
%   \end{minted}
% \end{example}
%
% To make your \LaTeX{} code more readable you might want to indent the code inside a |minted|
% environment.
% The option |gobble| removes these unnecessary whitespace characters from the output:
%
% \begin{example}
%   \begin{minted}[gobble=2,
%     showspaces]{python}
%     def boring(args = None):
%         pass
%   \end{minted}
%
%   versus
%
%   \begin{minted}[showspaces]{python}
%     def boring(args = None):
%         pass
%   \end{minted}
% \end{example}
%
%
% \subsection{Available options}
%
% \newcommand\appliesto[1]{\textsf{[For #1 only]}}
%
% Following is a full list of available options.
% For more detailed option descriptions please refer to the \pkg{fancyvrb} documentation,
% except where noted otherwise.
% \begin{optionlist}
%   \item[baselinestretch (+auto+\textbar dimension) (+auto+)]
%     Value to use as for baselinestretch inside the listing.
%   \item[bgcolor (string) (\emph{none})]
%     Background color of the listing.
%     Notice that the value of this option must \emph{not} be a color command. Instead, it must be a color
%     \emph{name}, given as a string, of a previously-defined color:
%
% \begin{example}
%   \definecolor{bg}{rgb}{0.95,0.95,0.95}
%   \begin{minted}[bgcolor=bg]{php}
%   <?php
%     echo "Hello, $x";
%   ?>
%   \end{minted}
% \end{example}
%
%   \item[firstline (integer) (+1+)]
%     The first line to be shown.
%     All lines before that line are ignored and do not appear in the output.
%   \item[firstnumber (+auto+\textbar integer) (+auto+ = 1)]
%     Line number of the first line.
%   \item[fontfamily (family name) (+tt+)]
%     The font family to use.
%     +tt+, +courier+ and +helvetica+ are pre-defined.
%   \item[fontseries (series name) (+auto+ -- the same as the current font)]
%     The font series to use.
%   \item[fontsize (font size) (+auto+ -- the same as the current font)]
%     The size of the font to use, as a size command, e.g. +\footnotesize+.
%   \item[fontshape (font shape) (+auto+ -- the same as the current font)]
%     The font shape to use.
%   \item[formatcom (command) (\emph{none})]
%     A format to execute before printing verbatim text.
%   \item[frame (+none+\textbar +leftline+\textbar +topline+\textbar +bottomline+\textbar +lines+\textbar +single+) (+none+)]
%     The type of frame to put around the source code listing.
%   \item[framerule (dimension) (+0.4pt+)]
%     Width of the frame.
%   \item[framesep (dimension) (\cmd\fboxsep)]
%     Distance between frame and content.
%   \item[funcnamehighlighting (boolean) (+true+)] \appliesto{PHP}
%     If +true+, highlights built-in function names.
%   \item[gobble (integer) (+0+)]
%     Remove the first $n$ characters from each input line.
%   \item[label (\char`\[string\char`\]string) (\emph{empty})]
%     Add a label to the top, the bottom or both of the frames around the code.
%     See the \pkg{fancyvrb} documentation for more information and examples.
%     \emph{Note:} This does \emph{not} add a +\label+ to the current listing.
%     To achieve that, use a floating environment (section \ref{sec:float}) instead.
%   \item[labelposition (+none+\textbar +topline+\textbar +bottomline+\textbar +all+) (+topline+, +all+ or \emph{none})]
%     Position where to print the label (see above;  default: +topline+ if one label is defined, +all+ if two are defined, \emph{none} else).
%     See the \pkg{fancyvrb} documentation for more information.
%   \item[lastline (integer) (\emph{last line of input})]
%     The last line to be shown.
%   \item[linenos (boolean) (+false+)]
%     Enables line numbers.
%     In order to customize the display style of line numbers, you need to redefine the +\theFancyVerbLine+
%     macro:
%
% \begin{example}
%   \renewcommand{\theFancyVerbLine}{\sffamily
%     \textcolor[rgb]{0.5,0.5,1.0}{\scriptsize
%     \oldstylenums{\arabic{FancyVerbLine}}}}
%
%   \begin{minted}[linenos,
%     firstnumber=11]{python}
%   def all(iterable):
%       for i in iterable:
%           if not i: 
%               return False
%       return True
%   \end{minted}
% \end{example}
%
%   \item[mathescape (boolean) (+false+)]
%     Enable \LaTeX{} math mode inside comments.
%     Do \emph{not} use spaces inside math mode -- they will be rendered like other full-width verbatim spaces.
%     Usage as in package \pkg{listings}.
%   \item[numberblanklines (boolean) (+true+)]
%     Enables or disables numbering of blank lines.
%   \item[numbersep (dimension) (+12pt+)]
%     Gap between numbers and start of line.
%   \item[obeytabs (boolean) (+false+)]
%     Treat tabs as tabs instead of converting them to spaces.
%   \item[resetmargins (boolean) (+false+)]
%     Resets the left margin inside other environments.
%   \item[rulecolor (color command) (\emph{black})]
%     The color of the frame.
%   \item[samepage (boolean) (+false+)]
%     Forces the whole listing to appear on the same page, even if it doesn't fit.
%   \item[showspaces (boolean) (+false+)]
%     Enables visible spaces: \verb*/visible spaces/.
%   \item[showtabs (boolean) (+false+)]
%     Enables visible tabs -- only works in combination with +obeytabs+.
%   \item[startinline (boolean) (+false+)] \appliesto{PHP}
%     Specifies that the code starts in PHP mode, i.e. leading +<?php+ is omitted.
%   \item[stepnumber (integer) (+1+)]
%     Interval at which line numbers appear.
%   \item[tabsize (integer) (+8+)]
%     The number of spaces a tab is equivalent to if +obeytabs+ is not active.
%   \item[texcl (boolean) (+false+)]
%     Enables \LaTeX{} code inside comments.
%     Usage as in package \pkg{listings}.
%   \item[xleftmargin (dimension) (+0+)]
%     Indentation to add before the listing.
%   \item[xrightmargin (dimension) (+0+)]
%     Indentation to add after the listing.
% \end{optionlist}
%
%
% \section{Defining shortcuts}
%
% Large documents with a lot of listings will nonetheless use the same source language and the
% same set of options for most listings.
% Always specifying all options is redundant, a lot to type and makes performing changes hard.
%
% \pkg{minted} therefore defines a set of commands that lets you define shortcuts for the highlighting
% commands.
% Each shortcut is specific for one programming language.
% \DescribeMacro{\newminted}
% |\newminted| defines a new alias for the |minted| environment:
%
% \begin{example}
%   \newminted{cpp}{gobble=2,linenos}
%
%   \begin{cppcode}
%     template <typename T>
%     T id(T value) {
%         return value;
%     }
%   \end{cppcode}
% \end{example}
%
% If you want to provide extra options on the fly, or override existing default options, you can do that, too:
%
% \begin{example}
%   \newminted{cpp}{gobble=2,linenos}
%
%   \begin{cppcode*}{linenos=false,
%                    frame=single}
%     int const answer = 42;
%   \end{cppcode*}
% \end{example}
%
% Notice the star ``|*|'' behind the environment name -- due to restrictions in \pkg{fancyvrb}'s handling
% of options, it is necessary to provide a \emph{separate} environment that accepts options, and the options
% are \emph{not} optional on the starred version of the environment.
%
% The default name of the environment is \meta{language}|code|.
% If this name clashes with another environment or if you want to choose an own name for another reason, you may
% do so by specifying it as the first argument: \cmd\newminted\oarg{environment name}\marg{language}\marg{options}.
%
% \DescribeMacro{\newmint}
% The above macro only defines shortcuts for the |minted| environment.
% The main reason is that the short command form |\mint| often needs different options -- at the very least, it
% will generally not use the |gobble| option.
% A shortcut for |\mint| is defined using \cmd\newmint\oarg{macro name}\marg{language}\marg{options}.
% The arguments and usage are identical to |\newminted|.
% If no \meta{macro name} is specified, \meta{language} is used.
%
% \begin{example}
%   \newmint{perl}{showspaces}
%
%   \perl/my $foo = $bar;/
% \end{example}
%
%
% \section{Known issues}
%
% \begin{itemize}
%   \item Extended characters do not work inside the |minted| environment, even in conjunction with
%     package \pkg{inputenc}.
%     \textbf{Solution}: Use |xelatex| instead of plain \LaTeX.
%   \item The command |\captionof| and other commands from the \pkg{caption} package produce an
%     error when used in combination with \pkg{minted}.
%     \textbf{Solution}: Load the \pkg{caption} package with the option |compatibility=false|.
%   \item \pkg{minted} doesn't work on Windows 7.
%     \textbf{Solution}: As a workaround, try the accepted answer at \href{http://tex.stackexchange.com/q/23458/42}{tex.stackexchange.com/q/23458}.
%   \item \ldots  See list at \href{http://code.google.com/p/minted/issues/list}{code.google.com/p/minted/issues}.
% \end{itemize}
%
% \StopEventually{
%   \addcontentsline{toc}{section}{Change History}
%   \PrintChanges}
%
%
% \section{Implementation}
%
%
% \subsection{Package options}
%
% \changes{1.7}{2011/09/12}{Options for float placement added}
%
% \begin{macro}{\minted@float@within}
%
% Define an option that controls the section numbering of the |listing| float.
%
%    \begin{macrocode}
\DeclareOption{chapter}{\def\minted@float@within{chapter}}
\DeclareOption{section}{\def\minted@float@within{section}}
%    \end{macrocode}
% \end{macro}
%
% Process package options.
%
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
%
% \subsection{System-specific settings}
%
% \changes{1.6}{2010/01/27}{Windows support added}
%
% Since we communicate with the ``outside world'', some operations must be defined system-dependently.
% ^^A TODO Fails on Windows 7, needs fixing; cf. http://code.google.com/p/minted/issues/detail?id=47
%
% \begin{macro}{\DeleteFile}
%
% Delete a file; we're careful in case someone has already defined this macro elsewhere.
%
%    \begin{macrocode}
\ifwindows
  \providecommand\DeleteFile[1]{\immediate\write18{del #1}}
\else
  \providecommand\DeleteFile[1]{\immediate\write18{rm #1}}
\fi
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TestAppExists}
%
% Check whether a given application exists on the system.
% Usage is a bit roundabout (should be fixed?) -- to test whether an application exists, use the
% following code:
%
% \begin{Verbatim}[gobble=2]
%   \TestAppExists{appname}
%   \ifthenelse{\boolean{AppExists}}
%     {app exists}{app doesn't exist}
% \end{Verbatim}
%
%    \begin{macrocode}
\newboolean{AppExists}
\newcommand\TestAppExists[1]{
  \ifwindows
%    \end{macrocode}
%
% On Windows, we need to use path expansion and write the result to a file.
% If the application doesn't exist, the file will be empty (except for a newline);
% otherwise, it will contain the full path of the application.
%
%    \begin{macrocode}
    \DeleteFile{\jobname.aex}
    \immediate\write18{for \string^\@percentchar i in (#1.exe #1.bat #1.cmd)
      do set >\jobname.aex <nul: /p x=\string^\@percentchar \string~$PATH:i>>\jobname.aex} %$
    \newread\@appexistsfile
    \immediate\openin\@appexistsfile\jobname.aex
    \expandafter\def\expandafter\@tmp@cr\expandafter{\the\endlinechar}
    \endlinechar=-1\relax
    \readline\@appexistsfile to \@apppathifexists
    \endlinechar=\@tmp@cr
    \ifthenelse{\equal{\@apppathifexists}{}}
     {\AppExistsfalse}
     {\AppExiststrue}
    \immediate\closein\@appexistsfile
    \DeleteFile{\jobname.aex}
\immediate\typeout{file deleted}
  \else
%    \end{macrocode}
%
% On Unix-like systems, we do a straightforward |which| test and create a file upon success,
% whose existence we can then check.
%
% \changes{1.7}{2010/02/16}{Removed unportable flag from Unix shell command}
%
%    \begin{macrocode}
    \immediate\write18{which #1 && touch \jobname.aex}
    \IfFileExists{\jobname.aex}
     {\AppExiststrue
      \DeleteFile{\jobname.aex}}
     {\AppExistsfalse}
  \fi}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Option processing}
%
% \begin{macro}{\minted@resetoptions}
%
% Reset options.
%
%    \begin{macrocode}
\newcommand\minted@resetoptions{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@defopt}
%
% Define an option internally and register it with in the
% |\minted@resetoptions| command.
%
%    \begin{macrocode}
\newcommand\minted@defopt[1]{
  \expandafter\def\expandafter\minted@resetoptions\expandafter{%
    \minted@resetoptions
    \@namedef{minted@opt@#1}{}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@opt}
%
% Actually use (i.e. read) an option value.
% Options are passed to |\detokenize| so that |\immediate\write18| will work properly.
%
%    \begin{macrocode}
\newcommand\minted@opt[1]{
  \expandafter\detokenize%
    \expandafter\expandafter\expandafter{\csname minted@opt@#1\endcsname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@opt}
%
% Define a generic option with an optional default argument.
% If a |key| option is specified without |=value|, the default is assumed.
%
%    \begin{macrocode}
\newcommand\minted@define@opt[3][]{
  \minted@defopt{#2}
  \ifthenelse{\equal{#1}{}}{
    \define@key{minted@opt}{#2}{\@namedef{minted@opt@#2}{#3}}}
   {\define@key{minted@opt}{#2}[#1]{\@namedef{minted@opt@#2}{#3}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@switch}
%
% Define an option switch (values are either |true| or |false|, and |true| may be
% omitted, e.g. |foobar| is the same as |foobar=true|).
%
%    \begin{macrocode}
\newcommand\minted@define@switch[3][]{
  \minted@defopt{#2}
  \define@booleankey{minted@opt}{#2}
   {\@namedef{minted@opt@#2}{#3}}
   {\@namedef{minted@opt@#2}{#1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@extra}
%
% Extra options are passed on to \pkg{fancyvrb}.
%
%    \begin{macrocode}
\minted@defopt{extra}
\newcommand\minted@define@extra[1]{
  \define@key{minted@opt}{#1}{
    \expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1=##1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@define@extra@switch}
%
% Extra switch options are also passed on to \pkg{fancyvrb}.
%
%    \begin{macrocode}
\newcommand\minted@define@extra@switch[1]{
  \define@booleankey{minted@opt}{#1}
   {\expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1}}
   {\expandafter\def\expandafter\minted@opt@extra\expandafter{%
      \minted@opt@extra,#1=false}}}
%    \end{macrocode}
% \end{macro}
%
% \noindent Actual option definitions.
%
% \changes{0.1.5}{2010/01/10}{Fixed \texttt{firstnumber} option}
% \changes{0.1.5}{2010/01/10}{Added \texttt{fillcolor} option}
% \changes{0.1.5}{2010/01/10}{Removed \texttt{caption} option}
% \changes{1.6}{2010/01/27}{Added font-related options}
% \changes{1.7}{2010/03/14}{Added PHP-specific options}
% \changes{1.7}{2011/01/04}{Added the \texttt{label} option}
%
%    \begin{macrocode}
\minted@define@switch{texcl}{-P texcomments}
\minted@define@switch{mathescape}{-P mathescape}
\minted@define@switch{linenos}{-P linenos}
\minted@define@switch{startinline}{-P startinline}
\minted@define@switch[-P funcnamehighlighting=False]%
  {funcnamehighlighting}{-P funcnamehighlighting}
\minted@define@opt{gobble}{-F gobble:n=#1}
\minted@define@opt{bgcolor}{#1}
\minted@define@extra{frame}
\minted@define@extra{framesep}
\minted@define@extra{framerule}
\minted@define@extra{rulecolor}
\minted@define@extra{numbersep}
\minted@define@extra{firstnumber}
\minted@define@extra{stepnumber}
\minted@define@extra{firstline}
\minted@define@extra{lastline}
\minted@define@extra{baselinestretch}
\minted@define@extra{xleftmargin}
\minted@define@extra{xrightmargin}
\minted@define@extra{fillcolor}
\minted@define@extra{tabsize}
\minted@define@extra{fontfamily}
\minted@define@extra{fontsize}
\minted@define@extra{fontshape}
\minted@define@extra{fontseries}
\minted@define@extra{formatcom}
\minted@define@extra{label}
\minted@define@extra@switch{numberblanklines}
\minted@define@extra@switch{showspaces}
\minted@define@extra@switch{resetmargins}
\minted@define@extra@switch{samepage}
\minted@define@extra@switch{showtabs}
\minted@define@extra@switch{obeytabs}
%    \end{macrocode}
%
%
% \subsection{Internal helpers}
%
% \begin{environment}{\minted@bgbox}
%
% Here, we define an environment that may be wrapped around a minted code to assign a background color.
%
% First, we need to define a new save box.
%
%    \begin{macrocode}
\newsavebox{\minted@bgbox}
%    \end{macrocode}
%
% Now we can define de environment that captures a code fragment inside a minipage and applies a background
% color.
%
%    \begin{macrocode}
\newenvironment{minted@colorbg}[1]{
%\setlength{\fboxsep}{-\fboxrule}
  \def\minted@bgcol{#1}
  \noindent
  \begin{lrbox}{\minted@bgbox}
  \begin{minipage}{\linewidth-2\fboxsep}}
 {\end{minipage}
  \end{lrbox}%
  \colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\minted@savecode}
%
% Save a code to be pygmentized to a file.
%
%    \begin{macrocode}
\newwrite\minted@code
\newcommand\minted@savecode[1]{
  \immediate\openout\minted@code\jobname.pyg
  \immediate\write\minted@code{#1}
  \immediate\closeout\minted@code}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@pygmentize}
%
% Pygmentize the file given as first argument (default: |\jobname.pyg|) using the
% options provided.
%
%    \begin{macrocode}
\newcommand\minted@pygmentize[2][\jobname.pyg]{
  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge
    \minted@opt{gobble} \minted@opt{texcl} \minted@opt{mathescape}
    \minted@opt{startinline} \minted@opt{funcnamehighlighting}
    \minted@opt{linenos} -P "verboptions=\minted@opt{extra}"
    -o \jobname.out.pyg #1}
  \immediate\write18{\minted@cmd}
  % For debugging, uncomment:
  %\immediate\typeout{\minted@cmd}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\begin{minted@colorbg}{\minted@opt@bgcolor}}
  \input{\jobname.out.pyg}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\end{minted@colorbg}}
  \DeleteFile{\jobname.out.pyg}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\minted@usedefaultstyle}
%
% Include the default stylesheet.
%
%    \begin{macrocode}
\newcommand\minted@usedefaultstyle{\usemintedstyle{default}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Public API}
%
% \begin{macro}{\usemintedstyle}
%
% Include stylesheet.
%
%    \begin{macrocode}
\newcommand\usemintedstyle[1]{
  \renewcommand\minted@usedefaultstyle{}
  \immediate\write18{pygmentize -S #1 -f latex > \jobname.pyg}
  \input{\jobname.pyg}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mint}
%
% Highlight a small piece of verbatim code.
%
%    \begin{macrocode}
\newcommand\mint[3][]{
  \DefineShortVerb{#3}
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \SaveVerb[aftersave={
    \UndefineShortVerb{#3}
    \minted@savecode{\FV@SV@minted@verb}
    \minted@pygmentize{#2}
    \DeleteFile{\jobname.pyg}}]{minted@verb}#3}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{minted}
%
% Highlight a longer piece of code inside a verbatim environment.
%
% \changes{1.7}{2011/08/30}{Fixed \texttt{tabsize} option}
%
%    \begin{macrocode}
\newcommand\minted@proglang[1]{}
\newenvironment{minted}[2][]
 {\VerbatimEnvironment
  \renewcommand{\minted@proglang}[1]{#2}
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \begin{VerbatimOut}[codes={\catcode`\^^I=12}]{\jobname.pyg}}%
 {\end{VerbatimOut}
  \minted@pygmentize{\minted@proglang{}}
  \DeleteFile{\jobname.pyg}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\inputminted}
%
% Highlight an external source file.
%
%    \begin{macrocode}
\newcommand\inputminted[3][]{
  \minted@resetoptions
  \setkeys{minted@opt}{#1}
  \minted@pygmentize[#3]{#2}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Command shortcuts}
%
% \changes{1.6}{2010/01/22}{Added command shortcuts}
%
% We allow the user to define shortcuts for the highlighting commands.
%
% \begin{macro}{\newminted}
%
% Define a new language-specific alias for the |minted| environment.
%
%    \begin{macrocode}
\newcommand\newminted[3][]{
%    \end{macrocode}
%
% First, we look whether a custom environment name was given as the first
% optional argument.
% If that's not the case, construct it from the language name (append ``|code|'').
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@envname{#2code}}
   {\def\minted@envname{#1}}
%    \end{macrocode}
%
% Now, we define two environments.
% The first takes no further arguments.
% The second, starred version, takes an extra argument that specifies option
% overrides.
%
%    \begin{macrocode}
  \newenvironment{\minted@envname}
   {\VerbatimEnvironment\begin{minted}[#3]{#2}}
   {\end{minted}}
  \newenvironment{\minted@envname *}[1]
   {\VerbatimEnvironment\begin{minted}[#3,##1]{#2}}
   {\end{minted}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newmint}
%
% Define a new language-specific alias for the |\mint| short form.
%
%    \begin{macrocode}
\newcommand\newmint[3][]{
%    \end{macrocode}
%
% Same as with |\newminted|, look whether an explicit name is provided.
% If not, take the language name as command name.
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@shortname{#2}}
   {\def\minted@shortname{#1}}
%    \end{macrocode}
%
% And define the macro.
%
%    \begin{macrocode}
  \expandafter\newcommand\csname\minted@shortname\endcsname[2][]{
    \mint[#3,##1]{#2}##2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newmintedfile}
%
% Finally, define a new language-specific alias for |\inputminted|.
%
%    \begin{macrocode}
\newcommand\newmintedfile[3][]{
%    \end{macrocode}
%
% Here, the default macro name (if none is provided) appends ``|file|'' to the language name.
%
%    \begin{macrocode}
  \ifthenelse{\equal{#1}{}}
   {\def\minted@shortname{#2file}}
   {\def\minted@shortname{#1}}
%    \end{macrocode}
%
% \dots and define the macro.
%
%    \begin{macrocode}
  \expandafter\newcommand\csname\minted@shortname\endcsname[2][]{
    \inputminted[#3,##1]{#2}{##2}}}
%    \end{macrocode}
%
% \end{macro}
%
%
% \subsection{Float support}
%
% \changes{0.1.5}{2010/01/10}{Added float support}
%
% \begin{environment}{listing}
%
% Defines a new floating environment to use for floated listings.
%
%    \begin{macrocode}
\@ifundefined{minted@float@within}
 {\newfloat{listing}{h}{lol}}
 {\newfloat{listing}{h}{lol}[\minted@float@within]}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\listingcaption}
%
% The name that is displayed before each individual listings caption and its number.
% The macro |\listingscaption| can be redefined by the user.
%
%    \begin{macrocode}
\newcommand\listingscaption{Listing}
%    \end{macrocode}
%
% The following definition should not be changed by the user.
%
%    \begin{macrocode}
\floatname{listing}{\listingscaption}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\listoflistingscaption}
%
% The caption that is displayed for the list of listings.
%
%    \begin{macrocode}
\newcommand\listoflistingscaption{List of listings}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\listoflistings}
%
% Used to produce a list of listings (like |\listoffigures| etc.).
% This may well clash with other packages (e.g. \pkg{listings}) but we choose to ignore this
% since these two packages shouldn't be used together in the first place.
%
%    \begin{macrocode}
\providecommand\listoflistings{\listof{listing}{\listoflistingscaption}}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Epilogue}
%
% Load default stylesheet -- but only if user has not yet loaded a custom
% stylesheet in the preamble.
%
%    \begin{macrocode}
\AtBeginDocument{
  \minted@usedefaultstyle}
%    \end{macrocode}
%
% Check whether LaTeX was invoked with |-shell-escape| option.
%
% \changes{1.7}{2011/01/21}{More robust detection of the \texttt{-shell-escape} option}
%    \begin{macrocode}
\AtEndOfPackage{
  \ifnum\pdf@shellescape=1\relax\else
    \PackageError{minted}
     {You must invoke LaTeX with the
      -shell-escape flag}
     {Pass the -shell-escape flag to LaTeX. Refer to the minted.sty
      documentation for more information.}\fi
%    \end{macrocode}
%
% Check whether \app{pygmentize} is installed.
%
%    \begin{macrocode}
  \TestAppExists{pygmentize}
  \ifAppExists\else
    \PackageError{minted}
     {You must have `pygmentize' installed
      to use this package}
     {Refer to the installation instructions in the minted
      documentation for more information.}
  \fi}
%    \end{macrocode}
%
% \Finale
